package com.pocket.insight.base.mql;




import com.pocket.insight.entity.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 描述：
 *
 * @author shuize
 * @create 2024/11/30
 */
public enum MDF implements DF {



    CONCAT(Type.TypeName.string, Type.TypeName.any, "s1", Type.TypeName.any, "s2"),
    ARRAY(Type.TypeName.array, Type.TypeName.any, "internal", Type.TypeName.any, "internal"),
    LEAST(Type.TypeName.number, Type.TypeName.number, "arg1", Type.TypeName.number, "arg2"),
    GREATEST(Type.TypeName.number, Type.TypeName.number, "arg1", Type.TypeName.number, "arg2"),
    JSON_BUILD_ARRAY(Type.TypeName.json, Type.TypeName.any, "internal", Type.TypeName.any, "internal"),
    MDF_WINDOW_REWRITE_BEGIN,

    ;

    MDF() {
    }

    MDF(MDF real) {
        this.real = real;
    }

    MDF(Type.TypeName type, DF.Argument... args) {
        this.signature = new DF.Signature(this.toString(), type, Arrays.asList(args));
    }

    MDF(Type.TypeName type, Type.TypeName argType, String placeholder) {
        this(type, new DF.Argument(argType, placeholder));
    }

    MDF(Type.TypeName type, Type.TypeName type0, String placeholder0, Type.TypeName type1, String placeholder1) {
        this(type, new DF.Argument(type0, placeholder0), new DF.Argument(type1, placeholder1));
    }

    MDF(Type.TypeName type, Type.TypeName type0, String placeholder0, Type.TypeName type1, String placeholder1, Type.TypeName type2, String placeholder2) {
        this(type, new DF.Argument(type0, placeholder0), new DF.Argument(type1, placeholder1), new DF.Argument(type2, placeholder2));
    }


    DF.Signature signature;
    MDF          real;
    static               Map<String, Operator> OPERATORS;
    private static final Integer               OPERATOR_PRES_MAX;

    static {
        OPERATORS = new HashMap();
        OPERATOR_PRES_MAX = 100000;
    }


    public static int getOperatorPrecedence(String op) {
        if (op != null && OPERATORS.containsKey(op.toUpperCase())) {
            return ((Operator) OPERATORS.get(op.toUpperCase())).precedence;
        }
        return OPERATOR_PRES_MAX.intValue();
    }

    public static MDF fromString(String op) {
        try {
            return valueOf(op.toUpperCase()).normalize();
        } catch (Exception exception) {
            return null;
        }
    }

    public MDF normalize() {
        if (this.real != null) {
            return this.real;
        }
        return this;
    }

    public static boolean isOperator(String op) {
        return op == null ? false : OPERATORS.containsKey(op.toUpperCase());
    }


    public static class Operator {
        public DF.Signature signature;
        public int          precedence;
    }

}
